package sy.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sy.jackson.SysProjectMixin;
import sy.jackson.SysUnitMixin;
import sy.util.MessageObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.model.SysAccount;
import sy.model.SysBuilding;
import sy.model.SysDivision;
import sy.model.SysProject;
import sy.model.SysRole;
import sy.model.SysUnit;
import sy.service.SysAccountServiceI;
import sy.service.SysBuildingServiceI;
import sy.service.SysDivisionServiceI;
import sy.service.SysProjectServiceI;
import sy.service.SysRoleServiceI;
import sy.service.SysUnitServiceI;
/**
 * @Description: 首页
 * @param 
 * @author     :wjy
 * Create Date : 2017年7月26日 
 */
@Controller
@SessionAttributes("currAccount")
@RequestMapping("accountController")
public class AccountController {
	
	@Autowired
	private SysAccountServiceI sysAccountService;
	@Autowired
	private SysRoleServiceI sysRoleService;
	private static final ObjectMapper objectMapper = new ObjectMapper();

	/************************** view controller ***************************/
	
	/**
	 * @Description: 首页视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<SysAccount> accountList = sysAccountService.list();
		
		model.addAttribute("accountList",accountList);
		return "userInfo/account/account";
	}
	/**
	 * 
	 * @Title: newObjectInfo
	 * @Description: TODO
	 * @param: @param model
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月16日 上午10:16:10
	 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		String uuid = UUID.randomUUID().toString();
		SysAccount account = new SysAccount();
		account.setId(uuid);
		
		List<SysRole> sysRoleList = sysRoleService.list();
		
		model.addAttribute("account",account);
		model.addAttribute("sysRoleList",sysRoleList);
		model.addAttribute("type",1);
		return "userInfo/account/accountFrom";
	}
	/**
	 * 
	 * @Title: editObjectInfo
	 * @Description: TODO
	 * @param: @param id
	 * @param: @param model
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月16日 上午10:16:21
	 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam String id,Model model){
		
		SysAccount account = sysAccountService.getSysAccountById(id);		
		List<SysRole> sysRoleList = sysRoleService.list();
		
		model.addAttribute("account",account);
		model.addAttribute("sysRoleList",sysRoleList);
		model.addAttribute("type",2);
		
		return "userInfo/account/accountFrom";
	}
	
	/************************** data controller ***************************/
	
	/**
	 * @Description: 删除
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String[] ids) {
		MessageObject messageObject = new MessageObject();
		
		try {
			sysAccountService.remove(ids);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
	
	/**
	 * @Description: 返回全部json数据
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {
		List<SysAccount> accountList = sysAccountService.list();
		try {
			return objectMapper.writeValueAsString(accountList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 
	 * @Title		:newObjectInfo
	 * @Description	:TODO
	 * @param		:@param account
	 * @param		:@param model
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:wujiayi
	 * @time		:2017年9月29日 上午10:31:53
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo( @RequestBody SysAccount account, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			sysAccountService.save(account);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
}
